package stz.flash.main.strobe.field;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.LabelField;
import stz.flash.option.strobe.StrobeOption;
import stz.flash.option.strobe.StrobeTimeIntervalManager;
import blackberry.util.field.DjControlBarChangeListener;
import blackberry.util.field.DjControlBar;
import blackberry.util.image.DjImageCalculator;
import blackberry.util.image.DjImageResizer;

public class StrobeControlBar extends Manager implements DjControlBarChangeListener {

	private static final Bitmap BITMAP_OF_HANDLE = DjImageResizer.fitIntoDisplay(
			Bitmap.getBitmapResource("Handle_Of_ControlBar.png"),
			38, 52, 
			480, 320, 
			DjImageCalculator.NONE, Bitmap.FILTER_BILINEAR);
	
	private static final Bitmap BITMAP_OF_BACKGROUND = DjImageResizer.fitIntoDisplay(
			Bitmap.getBitmapResource("Background_Of_ControlBar.png"),
			38, 266, 
			480, 320, 
			DjImageCalculator.NONE, Bitmap.FILTER_BILINEAR);
	
	private static DjControlBar controlBar = new DjControlBar(
			BITMAP_OF_HANDLE, 
			BITMAP_OF_BACKGROUND, 
			StrobeTimeIntervalManager.getIns().vectorSize(),
			StrobeOption.getIns().getTimeIntervalMainIndex(),
			AXIS_VERTICAL);
	
	private static LabelField timeIntervalLabel = new LabelField();

	private static final StrobeControlBar instance = new StrobeControlBar();
	
	private StrobeControlBar() {
		super(0);

		controlBar.setControlBarChangeListener(this);

		add(controlBar);
//		add(timeIntervalLabel);

		updateLabel();
	}
	
	public static StrobeControlBar getIns(){
		return instance;
	}
	
	public void controlbarChanged(int event, int currentIndexOfCtrlBar) {
		
		//Touch
		if (event == DjControlBarChangeListener.MOVE_WITH_INDEX) {
			
			StrobeTimeIntervalManager.getIns().setMainIndex(currentIndexOfCtrlBar);
			
		//Voulume Up
		} else if (event == DjControlBarChangeListener.MOVE_UPWARD) {
			
			StrobeTimeIntervalManager.getIns().decreaseMainIndex();
			
		//Voulume Down
		} else if (event == DjControlBarChangeListener.MOVE_DOWNWARD) {
			
			StrobeTimeIntervalManager.getIns().increaseMainIndex();
		}
		
		StrobeTimeIntervalManager.getIns().updateTimeInterval();
		updateLabel();
	}
	
	public void moveWithIndex(int index, boolean notify) {
		controlBar.moveWithIndex(index, notify);
	}
	
	public void moveUpward() {
		controlBar.moveUpward();
	}
	
	public void moveDownward() {
		controlBar.moveDownward();
	}

	private void updateLabel() {
		timeIntervalLabel.setText(String.valueOf(StrobeOption.getIns().getTimeInterval()));
	}

	protected void sublayout(int maxWidth, int maxHeight) {
		
		setExtent(getPreferredWidth(), getPreferredHeight());
		
		setPositionChild(controlBar, 
				0, 
				0);
		layoutChild(controlBar, 
				controlBar.getPreferredWidth(), 
				controlBar.getPreferredHeight());
	}
	
	public int getPreferredWidth() {
		return controlBar.getPreferredWidth();
	}
	
	public int getPreferredHeight() {
		return controlBar.getPreferredHeight();
	}
}
